package com.zy.controller;

import com.zy.entity.Carousel;
import com.zy.mapper.CarouselMapper;
import com.zy.mapper.PermissionMapper;
import com.zy.mapper.UserLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LittleDesire
 * @description
 * @date 2022-03-30 周三 14:27
 * @since 17
 */
@Controller
public class IndexController {
    @Autowired(required = false)
    private PermissionMapper pm;

    @Autowired(required = false)
    private UserLogMapper ulm;
    @RequestMapping
    public String index(HttpSession session,Model m){
        session.getAttribute("_code");
        return "login";
    }

    @RequestMapping("/home")
    public String home(Model m, HttpSession s){
        m.addAttribute("logs", ulm.query(s.getAttribute("user").toString()));
        m.addAttribute("banners",cm.show());
        return "home";
    }
    @GetMapping("/index")
    public String login(){
        return "index";
    }

    @GetMapping("/init") @ResponseBody
    public Map<String,Object> init(HttpSession s){
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> m1 = new HashMap<>();
        m1.put("title","首页");
        m1.put("href","home");
        map.put("homeInfo",m1);

        Map<String,String> m2 = new HashMap<>();
        m2.put("title","碳积分管理系统");
        m2.put("image","images/logo.png");
        m2.put("href","home");
        map.put("logoInfo",m2);

        Map<String,Object> menu = new HashMap<>();
        menu.put("title","默认左侧菜单");
        menu.put("icon","fa fa-address-book");
        menu.put("href","javascript:void(0)");
        menu.put("target","_self");
        menu.put("child",pm.menu(s.getAttribute("user").toString()));
        List<Map<String,Object>> menus = new ArrayList<>();
        menus.add(menu);
        map.put("menuInfo",menus);
        return map;
    }

    @Autowired(required = false)
    private CarouselMapper cm;
    @RequestMapping("carousel")
    public String carousel(Model m) {
        m.addAttribute("banners", cm.queryAll());
        return "carousel";
    }

    @RequestMapping("/api/carouseldb")@ResponseBody
    public Map<String, Object> carouseldb() {
        Map<String, Object> map = new HashMap<>();
        map.put("code", "0");
        map.put("msg", "");
        map.put("count", "3");
        map.put("data", cm.queryAll());
        return map;
    }


}
